package com.chlitina.store.modules.sms.utils;
import java.io.IOException;
import java.io.StringReader;
import java.security.MessageDigest;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import com.smslink.api.SmsClient;
//邮差短信
public class SMSSend {
	private static String Uid = "bo.zhou.79@gmail.com";
	private static String Key = "88586247";
	final public static int ONE_MES_length = 70;//一条短信的默认长度
	
	public static String send(String phone, String message){
		String result = "0";
		try {
			StringBuffer sendsms = SmsClient.sendsms(Uid, MD5(Key), message, phone, "", "");
			System.err.println(sendsms);
			result = getStatus(sendsms, "ax219:result");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	
	public static String getSmsReaminNum() {
		String num = "0";
		try {
			StringBuffer result = SmsClient.checkBalance(Uid, MD5(Key));
			num = getStatus(result, "ax219:balance");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return num;
	}
	
	private static String getStatus(StringBuffer xml, String type) {
		String result = "";
		try {
			DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();    
			DocumentBuilder builder = factory.newDocumentBuilder();   
			Document doc = builder.parse(new InputSource(new StringReader(xml.toString()))); 
			result = doc.getElementsByTagName(type).item(0).getTextContent(); 
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}  
		return result;
	}
	
	private static String getErrorMessage(int result) {
		switch (result) {
		case 0:
			return "缺少必须的参数";
		case -1:
			return "用户名或密码错误 ";
		case -2:
			return "手机号码格式不正确";
		case -3:
			return "日期格式不正确";
		default:
			return "短信发送异常";
		} 
	}
	
	public final static String MD5(String s) {
        char hexDigits[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};       
        try {
            byte[] btInput = s.getBytes();
            // 获得MD5摘要算法的 MessageDigest 对象
            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            // 使用指定的字节更新摘要
            mdInst.update(btInput);
            // 获得密文
            byte[] md = mdInst.digest();
            // 把密文转换成十六进制的字符串形式
            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
	
	public static void main(String[] args) {
		try {
//			String message = "1234567890123456789012345678901234567890123456789012345678901234567890";//71
//			String mobile = "18092262770";
//			StringBuffer sendsms = SmsClient.sendsms(Uid, MD5(Key), message, mobile, "", DateUtils.getDate("yyyy-MM-dd HH:mm:ss"));
//        	//StringBuffer getStatusById = SmsClient.getStatusById(username, password, "6376d806-440d-42bc-b40f-2ac0a7cb7cad",null ,null );
//        	//StringBuffer getStatusByTime = SmsClient.getStatusByTime(username, password, "","2013-04-10 08:00:00" ,"2013-04-19 09:00:57", null, null );
////        	StringBuffer getReplyByTime = SmsClient.getReplyByTime(username, password, "18629199483","","2013-10-08 10:00:00" ,"2013-12-08 16:30:00", "0", "1000" );
////        	StringBuffer checkBalance = SmsClient.checkBalance(username, password);
//////			
////        	System.out.println("resp="+sendsms.toString());
////			System.out.println("resp="+getStatusById.toString());
////			System.out.println("resp="+getStatusByTime.toString());
//        	System.out.println(sendsms);
//        	getStatus(sendsms, "ax219:result");
//        	StringBuffer checkBalance2 = SmsClient.checkBalance(Uid, MD5(Key));
//			System.out.println(checkBalance2);
//        	System.out.println(SmsProperty.getSoapFullUrl());
        	System.out.println(send("15902991399", "dd"));
		} catch (Exception e) {
		}
	}
}
